/** 拟态框 */
/**
 * @param {Object} option
 * @param {string} option.title 标题
 * @param {boolean} option.showCancel 是否显示取消按钮
 * @param {string} option.cancelText 取消按钮文字
 * @param {string} option.cancelColor 取消按钮颜色
 * @param {string} option.confirmText 确认按钮文字
 * @param {string} option.confirmColor 确认按钮颜色
 * @param {boolean} option.editable 是否显示输入框
 * @param {string} option.placeholderText 输入框文本内容占位
 * **/
const defaultOptions = {
    title: "温馨提示",
    showCancel: true,
    cancelText: "取消",
    cancelColor: "#999",
    confirmText: "确认",
    confirmColor: "#4c9cea",
    editable: false,
    placeholderText: "请输入",
};

class Modal {
    /** 默认 */
    confirm(title, option = {}, callback) {
        const config = {...defaultOptions, title: title, ...option, icon: "none"};
        return new Promise((resolve, reject) => {
            uni.showModal({
                ...config,
                success(res) {
                    if (res.confirm) {
                        resolve(true);
                    } else if (res.cancel) {
                        reject(false);
                    }
                },
                fail(err) {
                    reject(err);
                }
            });
        })
    }

    /** 输入框弹窗 */
    edit(title, option = {}, callback) {
        const config = {...defaultOptions, title: title, ...option, editable: true,};
        return new Promise((resolve, reject) => {
            uni.showModal({
                ...config,
                success(res) {
                    if (res.confirm) {
                        resolve(res.content);
                    } else if (res.cancel) {
                        resolve(false);
                    }
                },
                fail(err) {
                    reject(err);
                }
            });
        })
    }
}

export default new Modal();